package com.haierp.controller.yitao;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.haierp.exception.ErpCommonException;
import com.haierp.model.Item;
import com.haierp.service.IItemService;
import com.haierp.service.yitaoItem.YTIOuterItemService;
import com.haierp.util.JsonResult;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.utils.HaiJsonUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 一淘商品同步
 */
@Controller
@RequestMapping("/yitaoSyn")
//@RequestMapping("/haihuPushMessage")
public class YiTaoProductController extends BaseController {
    @Autowired
    private YTIOuterItemService ytiOuterItemService;
    @Autowired
    private IItemService itemService;

    @RequestMapping("/productSynIteYTao")
   // @RequestMapping("/haihupullOrder")
    @ResponseBody
    public  Object productSynToYiTo(String itemIds) {
        //itemIds="13";
        JsonResult<String> result = new JsonResult<>();
        StringBuilder sb = new StringBuilder();
        if(StringUtils.isNotEmpty(itemIds)){
            String s = itemIds.replace("&quot;", "\"");
            List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
            for(Long id:idList){
                try{
                    if(id!=null&&id!=0){
                        ytiOuterItemService.synItemYitao(id);
                    }
                }catch(ErpCommonException e){
                    sb.append(e.getErrorMsg()+",");
                }catch (Exception e) {
                    logger.error("同步异常，商品ID：" + id, e);
                    sb.append(id +"未知异常"+",");
                }
            }

        }else{
            return result.buildIsSuccess(false).buildMsg("没有商品");
        }
        String errorMsg = sb.toString();
        if(StringUtils.isEmpty(errorMsg)){
            return result.buildIsSuccess(true);
        }else{
            return result.buildIsSuccess(false).buildMsg(errorMsg);
        }
    }


    /**
     * 批量上架
     * @param
     * @return
     */
    @RequestMapping("/batchListingYiTao")
    @ResponseBody
    public Object batchListingYiTao(String itemIds) {
      /*  itemIds="12";*/
        JsonResult<String> result = new JsonResult<>();
        StringBuilder sb = new StringBuilder();
        if(StringUtils.isNotEmpty(itemIds)){
            String s = itemIds.replace("&quot;", "\"");
            List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
            if(CollectionUtils.isNotEmpty(idList)){
                List<Item> items = itemService.selectBatchIds(idList);
                for(Item item:items){
                    try {
                        if(item.getCompanyId()==1){
                            ytiOuterItemService.listingYitao(item);
                        }else{
                            throw new ErpCommonException("商品id="+"["+item.getId()+"]不属于如花商品不能执行上架有赞操作");
                        }
                    }
                    catch (ErpCommonException e) {
                        sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+":"+e.getErrorMsg());
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"未知错误;");
                    }
                }
            }else{
                return result.buildIsSuccess(false).buildMsg("没有商品");
            }
        }else{
            return result.buildIsSuccess(false).buildMsg("没有商品");
        }
        String errorMsg = sb.toString();
        if(StringUtils.isEmpty(errorMsg)){
            return result.buildIsSuccess(true);
        }else{
            return result.buildIsSuccess(false).buildMsg(errorMsg);
        }
    }

    /**
     * 批量下架
     * @param
     * @return
     */
    @RequestMapping("/batchDelistingYouzan")
    @ResponseBody
    public Object batchDelistingYouzan(String itemIds) {
       /* itemIds = "12";*/
        JsonResult<String> result = new JsonResult<>();
        StringBuilder sb = new StringBuilder();
        if(StringUtils.isNotEmpty(itemIds)){
            String s = itemIds.replace("&quot;", "\"");
            List<Long> idList = HaiJsonUtils.toBean(s, new TypeReference<List<Long>>(){});
            if(CollectionUtils.isNotEmpty(idList)){
                List<Item> items = itemService.selectBatchIds(idList);
                for(Item item:items){
                    try {
                        if(item.getCompanyId()==1){//如花商品同步到有赞
                            ytiOuterItemService.delistingYitao(item);
                        }else{
                            throw new ErpCommonException("商品id="+"["+item.getId()+"]不属于如花商品不能执行到有赞");
                        }
                    }
                    catch (ErpCommonException e) {
                        sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+":"+e.getErrorMsg());
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        sb.append("名称:"+item.getName()+"商品代码:"+item.getItemCode()+"未知错误;");
                    }
                }
            }else{
                return result.buildIsSuccess(false).buildMsg("没有商品");
            }
        }else{
            return result.buildIsSuccess(false).buildMsg("没有商品");
        }
        String errorMsg = sb.toString();
        if(StringUtils.isEmpty(errorMsg)){
            return result.buildIsSuccess(true);
        }else{
            return result.buildIsSuccess(false).buildMsg(errorMsg);
        }
    }


}
